package com.kgame5.xtank3d.app.gamecore {
import kgame5.kutil.KEvent;
import kgame5.kutil.T;

import com.kgame5.xtank3d.app.AppEnum;
import com.kgame5.xtank3d.app.gamecore.main.GameEngineComp;
import com.kgame5.xtank3d.app.gamecore.pane.clock.ClockComp;
import com.kgame5.xtank3d.app.gamecore.pane.readygo.ReadyGoComp;
import com.kgame5.xtank3d.app.gamecore.pane.result.LostComp;
import com.kgame5.xtank3d.app.gamecore.pane.result.WinComp;
import com.kgame5.xtank3d.app.gamecore.pane.tip.GameTipComp;
import com.kgame5.xtank3d.vo.level.CorpsVO;

import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.mediator.Mediator;

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

/**
 * @author kk
 * 这个是放在gbook.pageplay.main中的
 */
public class GamePlayMediator extends Mediator{
	public static const NAME:String="GamePlayMediator";
	
	private var readyComp:ReadyGoComp;//ready go的提示
	private var tipComp:GameTipComp;
	private var clockComp:ClockComp;
	
	private var winComp:WinComp;
	private var lostComp:LostComp;
	
	private var mainComp:GameEngineComp;//主游戏界面
	
	
	public function GamePlayMediator(mc_c:MovieClip){
		super(NAME);
		
		readyComp=new ReadyGoComp(mc_c.mc_readygo);
		readyComp.addEventListener(ReadyGoComp.EVENTTYPE_GO, onReadyComp_GO);
		
		tipComp=new GameTipComp(mc_c.mc_tip);
		
		clockComp=new ClockComp(mc_c.mc_clock);
		clockComp.addEventListener(ClockComp.EVENTTYPE_TIMEOVER, onClockComp_timerOver);
		
		mainComp=new GameEngineComp(mc_c.mc_main);
		
		//游戏引擎组件注意要添加事件
		mainComp.addEventListener(GameEngineComp.EVENTTYPE_NEW_CORPS, onMainComp_newCorps);
		mainComp.addEventListener(GameEngineComp.EVENTTYPE_MISSION_COMPLETE, onMainComp_missionComplete);
		mainComp.addEventListener(GameEngineComp.EVENTTYPE_GAMEOVER, onMainComp_gameOver);
		
		//游戏结果
		winComp=new WinComp(mc_c.mc_win);
		lostComp=new LostComp(mc_c.mc_lost);
		
		winComp.getReplayBtn().addEventListener(MouseEvent.CLICK, onResultComp_ClickReplay);
		lostComp.getReplayBtn().addEventListener(MouseEvent.CLICK, onResultComp_ClickReplay);
	}

	public override function listNotificationInterests() : Array {
		return [
			AppEnum.START_GAME_ENGINE,
			AppEnum.CLEAR_GAME_ENGINE
		];
	}

	public override function handleNotification(e : INotification) : void {
		switch(e.getName()){
			case AppEnum.START_GAME_ENGINE:
				mainComp.start();//引擎启动，填充Entity
				readyComp.start();//提示字 ready GO
				
				tipComp.clear();
				
				clockComp.Init();
		
				winComp.hide();
				lostComp.hide();
			break;
			
			case AppEnum.CLEAR_GAME_ENGINE:
				mainComp.clear();
				readyComp.clear();
				
				tipComp.clear();
				clockComp.Clear();
		
				winComp.hide();
				lostComp.hide();
			break;
		}
	}
	
	//--comp发送的事件--
	private function onMainComp_newCorps(e:KEvent):void{
		tipComp.showTip((e.info as CorpsVO).name);
		
	}
	private function onMainComp_missionComplete(e:KEvent):void{
		//this.sendNotification(AppEnum.MISSION_COMPLETE);
		winComp.show();
		clockComp.Stop();
	}
	private function onMainComp_gameOver(e:KEvent):void{
		//this.sendNotification(AppEnum.GAME_OVER);
		lostComp.show();
		clockComp.Stop();
	}
	
	//--
	private function onReadyComp_GO(e:KEvent):void{
		T.p("onReadyComp_GO");
		mainComp.startPlay();//TODO这个要等出完提示字再做
		clockComp.Start();
	}
	
	//--
	private function onResultComp_ClickReplay(e:Event):void{
		//清除游戏，重新游戏，这个要测试好~
		
		this.sendNotification(AppEnum.CLEAR_GAME_ENGINE);
		this.sendNotification(AppEnum.TURN_PAGE,AppEnum.PAGEID_COVER);
	}

	//--
	private function onClockComp_timerOver(e:Event):void{
		lostComp.show();
		clockComp.Stop();
		mainComp.deactivePlayer();
	}
}//end of class
}
